<?php if(!defined('OCTOMS')){header('HTTP/1.1 403');die('{"error":"forbidden"}');}
/*
 * @package       OctoMS
 * @subpackage	  WP-OctoMS
 * @link          http://octoms.com
 * @copyright     Copyright 2012, Valentino-Jivko Radosavlevici
 * 
 * Redistributions of files must retain the above copyright notice.
 * 
 * @since         OctoMS 0.0.1, WP-OctoMS 2.0.1
 */
	
	/*
	 * HTML widget
	 * 
	 * 
	 * @package OctoMS
	 * @subpackage WP-OctoMS
	 * @version 0.1
	 * 
	 * @author Valentino-Jivko Radosavlevici
	 */
	class w_html extends octoms
	{
		
		function __construct()
		{
			$this->_('view');
			$this->_('input');
			$this->_('session');
		}
		
		/**
		 * Tabs widget
		 * Transform a list into dynamic tabs. 
		 * The title consists of the characters before the first fullstop (.).
		 * 
		 * @content Insert the list (ol/ul) here.
		 * @author Valentino-Jivko Radosavlevici
		 */
		function tabs()
		{
			if (is_string($this->__content) && strlen($this->__content)>0)
			{
				if(
					(false !== strpos($this->__content, '<ul>') || false !== strpos($this->__content, '<ol>')) 
					&& 
					false !== preg_match_all('%<li>([^.]*?)\.(.*?)</li>%i', $this->__content, $result)
				)
				{
					if (count($result[1])>0)
					{
						// Start the holder
						print '<div class="tabs"><div class="head">';
						
						// Print each head element
						foreach ($result[1] AS $key => $el)
						{
							print '<div class="title" rel="h'.$key.'">'.$el.'</div>';
						}
						
						print '</div><div class="body">';
						
						// Print each body element
						foreach ($result[2] AS $key => $el)
						{
							print '<div class="tab" rel="t'.$key.'">'.$el.'</div>';
						}
						
						print '</div></div>';
						
						// Load the CSS and JS
						$this->template
							->scriptroot('html')
							->css('style')
							->js('script');
						
						// All done
						return;
					}
				}
				
				// Could not find match-> print the content
				print $this->__content;
			}
			
		}// end function tabs()
		
		/**
		 * Accordion widget
		 * Transform a list into an accordion. 
		 * The title consists of the characters before the first fullstop (.).
		 * 
		 * @param string $size [Accordion size]<ul>
		 * <li>full</li>
		 * <li>half</li>
		 * </ul>
		 * 
		 * @content Insert the list (ol/ul) here.
		 * @author Valentino-Jivko Radosavlevici
		 */
		function accordion($size='full')
		{
			if (!in_array($size, array('half','full')))
			{
				$size = 'full';
			}
			
			if (is_string($this->__content) && strlen($this->__content)>0)
			{
				if(
					(false !== strpos($this->__content, '<ul>') || false !== strpos($this->__content, '<ol>')) 
					&& 
					false !== preg_match_all('%<li>([^.]*?)\.(.*?)</li>%i', $this->__content, $result)
				)
				{
					if (count($result[1])>0)
					{	
						print '<div class="accordion">';
									
						// Print each accordion element
						foreach ($result[1] AS $key => $el)
						{
							print '<div class="a '.$size.'">
								<div class="head"><span class="arrow"></span>'.$el.'</div>
								<div class="body">'.$result[2][$key].'</div>
							</div>';
						}
						
						print '</div>';
						
						// Load the CSS and JS
						$this->template
							->scriptroot('html')
							->css('style')
							->js('script');
						
						// All done
						return;
					}
				}
				
				// Could not find match-> print the content
				print $this->__content;
			}
			
		}// end function accordion()
		
		/**
		 * Alert
		 * Display a closable alert box.
		 * 
		 * @param string $type [Alert type]<ul>
		 * <li>warning</li>
		 * <li>error</li>
		 * <li>success</li>
		 * <li>info</li>
		 * </ul>
		 * @content The alert message; No HTML, please
		 * @author Valentino-Jivko Radosavlevici
		 */
		function alert($type='warning')
		{
			// Set the type
			if (!in_array($type, array('warning','error','success','info')))
			{
				$type = 'warning';
			}
			
			// Load the CSS and JS
			$this->template
				->scriptroot('html')
				->css('style')
				->js('script');
			
			// Load the input library
			$this->_('input');
			
			// Print the alert box
			print sprintf(
				'<div class="alert %s"><span class="arrow"></span>%s<span class="close"></span></div>',
				$type,
				trim($this->input->clean_tags($this->__content),true)
			);
			
		}// end function alert()
		
		/**
		 * Buttons
		 * Display anchors as buttons
		 * 
		 * @param string $color [The button color]<ul>
		 * <li>red</li>
		 * <li>orange</li>
		 * <li>yellow</li>
		 * <li>green</li>
		 * <li>blue</li>
		 * <li>indigo</li>
		 * <li>violet</li>
		 * </ul>
		 * @param string $text [The button text]
		 * @param string $link [Link (optional)]
		 * 
		 * @author Valentino-Jivko Radosavlevici
		 */
		function button($color='red',$text='',$link='')
		{	
			// Format the text
			if (typeof($text) != 'string' || $text == ''){$text = '#';}
			
			// Format the link
			$link = strval($link);
			if (strlen($link) == 0){$link = '#';}
			
			// Restrict the color
			if (!in_array(
				$color, 
				array(
					'red',
					'orange',
					'yellow',
					'green',
					'blue',
					'indigo',
					'violet'
				)
			)){$color = 'red';}
			
			// Load the CSS and JS
			$this->template->scriptroot('html')->css('style');
				
			// Print the button
			print a($text,$link,array('class'=>$color.' button'));
			
		}// end function button()
		
		/**
		 * Text
		 * Add arbitrary HTML or text.
		 * 
		 * @content HTML text
		 * @author Valentino-Jivko Radosavlevici
		 */
		function text()
		{
			print $this->__content;
			
		}// end function text()
		
		/**
		 * Class wrapper
		 * Wrap content around a DIV HTML element with the given class.
		 * 
		 * @param string $class [CSS class to use for this wrapper]
		 * @content HTML or text to use inside the div
		 * @author Valentino-Jivko Radosavlevici
		 */
		function clss($class=null)
		{
			// Set the class
			if (is_null($class))
			{
				$class = array();
			}
			else 
			{
				$class = array('class'=>$class);
			}
			
			// Print the div
			print tag('div',$class,$this->__content);
			
		}// end function clss()
		
		/**
		 * HR
		 * Create a HR HTML element with the given class.
		 * 
		 * @param string $class [Select the Horizontal Rule's class]<ul>
		 * <li>dash</li>
		 * <li>line</li>
		 * </ul>
		 * 
		 * @author Valentino-Jivko Radosavlevici
		 */
		function hr($class=null)
		{
			// A fixed set of options
			if (!in_array($class, array('dash','line')))
			{
				$class = 'dash';
			}
			
			// Print the HR
			print tag('hr',array('class'=>$class),null,true);
			
		}// end function hr()
		
		/**
		 * Any HTML element
		 * Print an HTML element with the set options
		 * 
		 * @param string $name [HTML element's name (ex. div, span...)]
		 * @param array $options [Element options such as class, id etc.]<ul>
		 * <li>class</li>
		 * <li>id</li>
		 * <li>...</li>
		 * </ul>
		 * @param bool $short false [Short HTML element like HR?]
		 * 
		 * @content The element's enclosed content
		 * @author Valentino-Jivko Radosavlevici
		 */
		function element($name=null,$options=array(),$short=false)
		{
			// The name is mandatory
			if (is_null($name))
			{
				return;
			}
			
			// Print the tag
			print tag($name,$options,$this->__content,$short);
			
		}// end function element()
		
		/**
		 * Contact form
		 * Print a HTML contact form with ReCaptcha user verification.
		 * 
		 * @param string $email {default_email} [The e-mail address (mandatory)]
		 * @param string $publicKey [The <a href="http://www.google.com/recaptcha" target="_blank">ReCaptcha</a> public key]
		 * @param string $privateKey [The ReCaptcha private key]
		 * @param string $theme [ReCaptcha Theme]<ul>
		 * <li>red</li>
		 * <li>white</li>
		 * <li>blackglass</li>
		 * <li>clean</li>
		 * </ul>
		 * 
		 * @author Valentino-Jivko Radosavlevici
		 */
		function contact($email=null,$publicKey=null,$privateKey=null,$theme='red')
		{
			
			// Verify the e-mail
			if (is_null($email))
			{
				// Get the user information
				$info = wp_get_current_user();
				
				// Get the e-mail
				$email = @$info->user_email;
				
				// None? This can't be!
				if (empty($email))
				{
					return false;
				}
			}

			// Get the public key
			if (is_null($publicKey))
			{
				$publicKey = '6Lelm8oSAAAAABmEISKDSgYQ3IyyzpiAsi8Yw2vr';
			}
			
			// Get the private key
			if (is_null($privateKey))
			{
				$privateKey = '6Lelm8oSAAAAAHOmLrq3_pqJ8I6xOO4fBbiSx4IG';
			}
			
			// Get the theme
			if (!in_array($theme, array('red','white','blackglass','clean')))
			{
				$theme = 'red';
			}

			// Get the reCaptcha object
			$this->_('captcha',OMS_L);
			
			// Load the JS
			$this->template
				->scriptroot()
				->js('wp-octoms')
				->scriptroot('html')
				->css('style');
			
			// Initiate the captcha
			$this->captcha->init(	
				array($this,'_captcha'),
				$publicKey,
				$privateKey,
				array(
					'email'=>$email
				)
			);
			
			// Load the form
			$this->view->display(
				'contact',
				array(
					'captcha' => $this->captcha->html($theme)
				)
			);
			
			// Listen for the keyword #*# for the AJAX interface
			if ($this->input->post('#*#'))
			{
				// Prepare the API response array
				$api = array();
				
				// Set the status
				is_null($api['_status_'] = $this->template->_get1('api_status'))?($api['_status_'] = array('Done.',true)):null;
				
				// Set the filters
				$api['_filters_'] =(is_null($api['_filters_'] = $this->template->_get1('api_filters')))?array():array_flat($api['_filters_']);
				
				// Set the content
				!is_null($cnt = $this->template->_get1('api_content'))?($api['_content_']=$cnt):null;
				
				// This is a plain JSON response
				while(@ob_end_clean());
				
				// We <3 JSON
				print json_encode($api);
				
				// All done
				exit();
			}
			
		}// end function contact()
		
		/**
		 * Style
		 * Output any CSS style
		 * 
		 * @author Valentino-Jivko Radosavlevici
		 */
		function style()
		{
			print css($this->__content,null,false);
			
		}// end function style()
		
		function _h_contact()
		{
			// Get the user information
			$info = wp_get_current_user();
			
			// Get the e-mail
			return array(
				'default_email'=>@$info->user_email
			);
		}
		
		/**
		 * Captcha helper
		 * 
		 * @author Valentino-Jivko Radosavlevici
		 */
		function _captcha($r)
		{
			if($r->is_valid)
			{
				// Validate the form
				if (true === $v = validate())
				{
					// To
					$to = $r->data['email'];
					
					// Subject
					$subject = 'Website contact';
					
					// Get the data
					$name = $this->input->post('uname','all');
					$email = $this->input->post('email','all');
					$content = $this->input->post('content','all');
					
					// E-mail address format
					$format = 'User "%s" (%s) contacted us.'."\n\n".'%s'."\n\n".'IP: %s';
					
					// Send the e-mail address
					if (@mail($to, $subject, sprintf($format,$name,$email,$content,$_SERVER['REMOTE_PORT'])))
					{
						$this->template->_api('Thank you!')
							->_api(array('form_validate'))
							->_api_cnt(array());
					}
					else 
					{
						$this->template->_api('Please try again later.','warning')
							->_api(array('form_validate'))
							->_api_cnt(array());
					}
				}
				else 
				{
					$this->template
						->_api('Verify your form.',false)
						->_api(array('form_validate'))
						->_api_cnt($v);
				}
			}
			else
			{
				$this->template->_api($r->error,false);
			}
		}// end function _captcha()
		
		/**
		 * Columns
		 * Enclose content into a CSS column with the set width.
		 * The total width for columns in a row is 16.
		 * 
		 * @param int $width [Select column width]<ul>
		 * <li>1</li>
		 * <li>2</li>
		 * <li>3</li>
		 * <li>4</li>
		 * <li>5</li>
		 * <li>6</li>
		 * <li>7</li>
		 * <li>8</li>
		 * <li>9</li>
		 * <li>10</li>
		 * <li>11</li>
		 * <li>12</li>
		 * <li>13</li>
		 * <li>14</li>
		 * <li>15</li>
		 * <li>16</li>
		 * </ul>
		 * @param bool $alpha false [Is this the first column in the row?]
		 * @param bool $omega false [Is this the last column in the row?]
		 * @param int $offset [Select column offset]<ul>
		 * <li>0</li>
		 * <li>1</li>
		 * <li>2</li>
		 * <li>3</li>
		 * <li>4</li>
		 * <li>5</li>
		 * <li>6</li>
		 * <li>7</li>
		 * <li>8</li>
		 * <li>9</li>
		 * <li>10</li>
		 * <li>11</li>
		 * <li>12</li>
		 * <li>13</li>
		 * <li>14</li>
		 * <li>15</li>
		 * </ul>
		 * 
		 * @content Set the content inside this column
		 * @author Valentino-Jivko Radosavlevici
		 */
		function columns($width=1,$alpha=false,$omega=false,$offset=0)
		{
			// Set the columns
			$columns = array(
				'one','two','three','four','five','six','seven','eight',
				'nine','ten','eleven','twelve','thirteen','fourteen','fifteen','sixteen'
			);
			
			// Set the column width
			$width = intval($width);
			if (!in_array(($width-1), array_keys($columns))) $width = 1;

			// Set the column offset
			$offset = intval($offset);
			if ($offset<0 || $offset>15) $offset = 0;
			
			// Set the width class
			$class = ($columns[$width-1]).' columns';
			
			// Set the offset class
			if ($offset != 0) $class .= ' offset-by-'.($columns[$offset-1]);
			
			// Use alpha?
			if ($alpha) $class .= ' alpha';
			
			// Use omega?
			if ($omega) $class .= ' omega';
			
			// Display the div
			print tag('div',array('class'=>$class),$this->__content);
			
		}// end function columns()
		
	}// end class w_html
	
	
/* End Of File <w_html.inc> */